package digui.tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Vector;

/**
 * @author cyt
 * @create 2020-05-29 18:00
 */
public class CommonTree {

    public static void main(String[] args) {

        CommonTreeNode node = new CommonTreeNode(1);
        CommonTreeNode node2 = new CommonTreeNode(2);
        CommonTreeNode node3 = new CommonTreeNode(3);
        CommonTreeNode node4 = new CommonTreeNode(4);
        CommonTreeNode node5 = new CommonTreeNode(5);
        CommonTreeNode node6 = new CommonTreeNode(6);
        CommonTreeNode node7 = new CommonTreeNode(7);
        CommonTreeNode node8 = new CommonTreeNode(8);

        Vector<CommonTreeNode> trees = new Vector<>();

        trees.add(node2);
        trees.add(node3);
        trees.add(node4);
        trees.add(node5);
        node.setVector(trees);

        Vector<CommonTreeNode> trees2 = new Vector<>();

        trees2.add(node6);
        trees2.add(node7);
        trees2.add(node8);


        node4.setVector(trees2);

        /*
         *           1
         *     2   3   4   5
         *  6  7   8
         * */
        CommonTreeVis(node);
    }

    /**
     * 普通树的遍历
     *
     * @param node
     */
    static void CommonTreeVis(CommonTreeNode node) {
        Queue<CommonTreeNode> queue = new LinkedList<>();
        queue.add(node);
        while (!queue.isEmpty()) {
            CommonTreeNode peek = queue.peek();
            queue.remove();
            System.out.println(peek.val);
            if (peek.vector != null) {
                for (CommonTreeNode treeNode : peek.vector) {
                    if (treeNode != null) {
                        queue.add(treeNode);
                    }
                }
            }
        }

    }
}

class CommonTreeNode {
    int val;
    Vector<CommonTreeNode> vector;

    public CommonTreeNode(int val) {
        this.val = val;
    }

    public void setVal(int val) {
        this.val = val;
    }

    public void setVector(Vector<CommonTreeNode> vector) {
        this.vector = vector;
    }
}
